﻿using LogCenterService.Common.Extension;
using LogCenterService.Common.Interfaces;
using LogCenterService.DbCore;
using LogCenterService.Models;
using LogCenterService.Models.Entity;
using LogCenterService.Models.QueryParam;
using LogCenterService.Models.ViewDTO;
using SqlSugar;

namespace LogCenterService.Services;

public interface ILogService : IPagingQuery<LogViewDTO, LogQueryParam>
{
    /// <summary>
    /// 插入日志
    /// </summary>
    /// <param name="logs"></param>
    /// <returns></returns>
    Task<int> InsertableAsync(List<SYS_Log> logs);

    /// <summary>
    /// 根据追踪id查询
    /// </summary>
    /// <param name="TrackingId">追踪id</param>
    /// <returns></returns>
    Task<List<LogViewDTO>> QueryByTrackingIdAsync(string TrackingId);

    /// <summary>
    /// 获取所有的版本号、来源
    /// </summary>
    /// <returns></returns>
    Task<(List<string>, List<string>, List<string>)> GetVersionAndSourceAsync();
}

public class LogService : DbContext, ILogService
{
    /// <summary>
    /// 插入日志
    /// </summary>
    /// <param name="logs"></param>
    /// <returns></returns>
    public async Task<int> InsertableAsync(List<SYS_Log> logs)
    {
        return await Db.Insertable(logs).UseParameter().ExecuteCommandAsync();
    }

    /// <summary>
    /// 分页查询
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<TableModel<LogViewDTO>> PagingQueryAsync(LogQueryParam param)
    {
        var result = await Db.Queryable<SYS_Log>()
            .WhereIF(!SqlFunc.IsNullOrEmpty(param.StartTime) && !SqlFunc.IsNullOrEmpty(param.EndTime), it => SqlFunc.Between(it.CreateTime, param.StartTime, param.EndTime))
            .WhereIF(!SqlFunc.IsNullOrEmpty(param.Message), it => it.Message.Contains(param.Message.Trim()))
            .WhereIF(!SqlFunc.IsNullOrEmpty(param.Level), it => it.Level == param.Level.Trim())
            .WhereIF(!SqlFunc.IsNullOrEmpty(param.TrackingId), it => it.TrackingId == param.TrackingId.Trim())
            .WhereIF(!SqlFunc.IsNullOrEmpty(param.Source), it => it.Source == param.Source.Trim())
            .WhereIF(!SqlFunc.IsNullOrEmpty(param.Path), it => it.Path == param.Path.Trim())
            .WhereIF(!SqlFunc.IsNullOrEmpty(param.Version), it => it.Version == param.Version.Trim())
            .Select(a => new LogViewDTO(), true)
            .OrderBy(a => a.CreateTime, param.OrderByType)
            .PageListAsync(param);
        return result;
    }

    /// <summary>
    /// 根据追踪ID查询
    /// </summary>
    /// <param name="TrackingId"></param>
    /// <returns></returns>
    public async Task<List<LogViewDTO>> QueryByTrackingIdAsync(string TrackingId)
    {
        var result = await Db.Queryable<SYS_Log>()
            .WhereIF(!SqlFunc.IsNullOrEmpty(TrackingId), it => it.TrackingId == TrackingId)
            .Select(a => new LogViewDTO(), true)
            .OrderBy(a => a.CreateTime)
            .ToListAsync();
        return result;
    }

    /// <summary>
    /// 获取所有的版本号、来源
    /// </summary>
    /// <returns></returns>
    public async Task<(List<string>, List<string>, List<string>)> GetVersionAndSourceAsync()
    {
        var Version = await Db.Queryable<SYS_Log>().Distinct()
            .Where(it => !SqlFunc.IsNullOrEmpty(it.Version))
            .OrderBy(it => it.Version)
            .Select(it => it.Version)
            .ToListAsync();

        var Source = await Db.Queryable<SYS_Log>().Distinct()
            .Where(it => !SqlFunc.IsNullOrEmpty(it.Source))
            .OrderBy(it => it.Source)
            .Select(it => it.Source)
            .ToListAsync();

        var Path = await Db.Queryable<SYS_Log>().Distinct()
            .Where(it => !SqlFunc.IsNullOrEmpty(it.Path))
            .OrderBy(it => it.Path)
            .Select(it => it.Path)
            .ToListAsync();

        return (Version, Source, Path);
    }
}